package com.budwk.app.sqfwV2.generator.dulaying;

import cn.hutool.core.date.DateUtil;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowBusinessParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwV2.dto.dulaying.DuLaYingPropagandaOrScreeningPdfDTO;
import com.budwk.app.sqfwV2.enums.PropagandaOrScreeningEnum;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.ClassUtil;
import com.budwk.app.util.poiUtils.CommonFormOfficeUtils;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.deepoove.poi.data.PictureRenderData;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-03-06
 * <p>
 * 都拉营街道宣传/排查记录 DuLaYingPropagandaOrScreening
 * 宣传记录表.docx
 */
public class PropagandaPdfGenerator extends BasePdfGenerator {

    private String theme;

    /**
     * 判断使用使用这个generator生成pdf
     *
     * @param pdfDTO 创建pdf数据素材
     * @return 结果
     */
    @Override
    public boolean pdfCreateConditionCheck(PdfGenerateParam pdfDTO) {
        DuLaYingPropagandaOrScreeningPdfDTO dto = (DuLaYingPropagandaOrScreeningPdfDTO)
                pdfDTO.getAbstractCustomerPdfCreateField();
        if (PropagandaOrScreeningEnum.PROPAGANDA.getName().equals(dto.getType())) {
            return true;
        }
        return false;
    }

    @Override
    public String getTemplatePath(PdfGenerateParam pdfGenerateParam) {
        return "duLaYing/propaganda.docx";
    }

    @Override
    public void customerPopularPdfParam(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        // 在这里对一些数据进行填充 进行复杂的业务代码逻辑
        DuLaYingPropagandaOrScreeningPdfDTO fieldDTO = (DuLaYingPropagandaOrScreeningPdfDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();
        paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getDate), DateUtil.format(fieldDTO.getDate(), "yyyy-MM-dd"));

        String theme = this.getTheme();
        switch (theme) {
            case "消防": {
                paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getTheme), theme);
                paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getPropagandaInvestigationAndSummary),
                        fieldDTO.getPropagandaInvestigationAndSummary().getFireFighting());
                break;
            }
            case "防诈": {
                paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getTheme), theme);
                paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getPropagandaInvestigationAndSummary),
                        fieldDTO.getPropagandaInvestigationAndSummary().getAntiFraud());
                break;
            }
            case "反邪": {
                paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getTheme), theme);
                paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getPropagandaInvestigationAndSummary),
                        fieldDTO.getPropagandaInvestigationAndSummary().getCounterevil());
                break;
            }
            case "禁毒": {
                paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getTheme), theme);
                paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getPropagandaInvestigationAndSummary),
                        fieldDTO.getPropagandaInvestigationAndSummary().getDrugControl());
                break;
            }
            case "交通安全": {
                paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getTheme), theme);
                paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getPropagandaInvestigationAndSummary),
                        fieldDTO.getPropagandaInvestigationAndSummary().getTrafficSafety());
                break;
            }
            case "垃圾分类": {
                paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getTheme), theme);
                paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getPropagandaInvestigationAndSummary),
                        fieldDTO.getPropagandaInvestigationAndSummary().getGarbageSorting());
                break;
            }
            case "医保": {
                paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getTheme), theme);
                paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getPropagandaInvestigationAndSummary),
                        fieldDTO.getPropagandaInvestigationAndSummary().getMedicalInsurance());
                break;
            }
            case "党风廉政": {
                paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getTheme), theme);
                paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getPropagandaInvestigationAndSummary),
                        fieldDTO.getPropagandaInvestigationAndSummary().getPartyConductCleanGovernment());
                break;
            }
            case "食品安全": {
                paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getTheme), theme);
                paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getPropagandaInvestigationAndSummary),
                        fieldDTO.getPropagandaInvestigationAndSummary().getFoodSafety());
                break;
            }
            case "关爱未成年人": {
                paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getTheme), theme);
                paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getPropagandaInvestigationAndSummary),
                        fieldDTO.getPropagandaInvestigationAndSummary().getCaringForMinors());
                break;
            }
            case "治风": {
                paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getTheme), theme);
                paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getPropagandaInvestigationAndSummary),
                        fieldDTO.getPropagandaInvestigationAndSummary().getControllingWind());
                break;
            }
        }

        if (StringUtils.isBlank(fieldDTO.getPeers())) {
            CompleteFlowBusinessParam param = (CompleteFlowBusinessParam) pdfGenerateParam.getBusinessParam();
            Sqfw_formSummary_new formSummaryNew = param.getFormSummary();
            paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getPeers), formSummaryNew.getCreateUserName());
        } else {
            Cnd cnd = Cnd.NEW();
            cnd.and(Sys_user::getId, "in", List.of(fieldDTO.getPeers().split(",")));
            StringBuilder stringBuilder = new StringBuilder();
            List<Sys_user> userList = NutzIocUtils.getBean(SysUserService.class).query(cnd);
            for (Sys_user user : userList) {
                if (stringBuilder.length() == 0) {
                    stringBuilder.append(user.getUsername());
                } else {
                    stringBuilder.append("、").append(user.getUsername());
                }
            }
            paramMap.put(ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getPeers), stringBuilder.toString());
        }

        // 图片处理
        String images = fieldDTO.getImages();
        String fieldName = ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getImages);
        List<String> pictureUrlList = images == null ? Arrays.asList("") : Arrays.asList(images.split(","));
        List<Map<String, PictureRenderData>> pictures = CommonFormOfficeUtils.addWordPicture(pictureUrlList, fieldName, 8.0F);
        paramMap.put(fieldName + "s", pictures);
    }

    public String getTheme() {
        return theme;
    }

    public void setTheme(String theme) {
        this.theme = theme;
    }
}
